package com.gjy.design.builder;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-11-11 09:27:20
 */
public class NutritionFacts {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    private NutritionFacts(Builder b) {
        servingSize = b.servingSize;
        servings = b.servings;
        calories = b.calories;
        fat = b.fat;
        sodium = b.sodium;
        carbohydrate = b.carbohydrate;
    }

    @Override
    public String toString() {
        return "NutritionFacts{" +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calories=" + calories +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }

    public static class Builder {
        private final int servingSize;     // 必填
        private final int servings;        // 必填
        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

        public Builder(int servingSize, int servings) {
            if (servingSize <= 0 || servings <= 0) {
                throw new IllegalArgumentException("servingSize and servings must be > 0");
            }
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder calories(int val) {
            this.calories = val;
            return this;
        }

        public Builder fat(int val) {
            this.fat = val;
            return this;
        }

        public Builder sodium(int val) {
            this.sodium = val;
            return this;
        }

        public Builder carbohydrate(int val) {
            this.carbohydrate = val;
            return this;
        }

        public NutritionFacts build() {
            if (calories < 0 || fat < 0 || sodium < 0 || carbohydrate < 0) {
                throw new IllegalStateException("nutrition values must be >= 0");
            }
            return new NutritionFacts(this);
        }
    }

}
